package com.yb.guigu.pick.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yb.guigu.pick.auth.bean.dto.request.SysPermissionAddInfo;
import com.yb.guigu.pick.auth.bean.vo.request.SysPermissionCreateVo;
import com.yb.guigu.pick.auth.entity.SysPermission;
import com.yb.guigu.pick.auth.entity.SysRoute;
import com.yb.guigu.pick.auth.mapper.SysPermissionMapper;
import com.yb.guigu.pick.auth.mapper.SysRouteMapper;
import com.yb.guigu.pick.auth.service.ISysPermissionService;
import com.yb.guigu.pick.core.bean.GuiguResult;
import com.yb.guigu.pick.core.constant.PermissionConstant;
import com.yb.guigu.pick.core.exception.GuiguBusinessException;
import com.yb.guigu.pick.core.publisher.GuiguPickEventPublisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚兵
 * @since 2023-06-06
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {

    @Autowired
    SysRouteMapper routeMapper;


    @Autowired
    GuiguPickEventPublisher guiguPickEventPublisher;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GuiguResult create(SysPermissionCreateVo sysPermissionCreateVo) {

         if(!StringUtils.hasText(sysPermissionCreateVo.getPermissionCode())){
             throw new GuiguBusinessException("权限编码不能为空");
         }

         if(!StringUtils.hasText(sysPermissionCreateVo.getPermissionName())){
             throw new GuiguBusinessException("权限名称不能为空");
         }
        Integer routeId = sysPermissionCreateVo.getRouteId();


         //根据routeId找到其所有的父节点
        SysRoute sysRoute=null;
        String routeIdPath=sysPermissionCreateVo.getRouteIdPath();
        if(Objects.nonNull(routeId)){
             sysRoute = routeMapper.selectById(routeId);
             if(Objects.isNull(sysRoute)){
                 throw new GuiguBusinessException("权限指定路由不存在，请重新指定");
             }
             routeIdPath=sysRoute.getRouteIdPath();
        }
        SysPermission sysPermission=new SysPermission();

        //如果是接口权限
        if(StringUtils.hasText(sysPermissionCreateVo.getPath())){
            sysPermission.setType(PermissionConstant.Type.INTERFACE.getValue());
            sysPermission.setPath(sysPermissionCreateVo.getPath());
            sysPermission.setPermissionCode(PermissionConstant.Type.INTERFACE.getPrefix()+sysPermissionCreateVo.getPermissionCode());
        }else{
            //如果是普通权限
            sysPermission.setType(PermissionConstant.Type.SIMPLE.getValue());
            sysPermission.setPermissionCode(PermissionConstant.Type.SIMPLE.getPrefix()+sysPermissionCreateVo.getPermissionCode());
        }

        //如果是路由或者菜单权限
        if(Objects.nonNull(sysRoute)){
            sysPermission.setRouteId(routeId);
            sysPermission.setRouteName(sysRoute.getName());
            sysPermission.setRoutePath(sysRoute.getPath());
            PermissionConstant.Type type = Objects.requireNonNull(PermissionConstant.Type.parse(sysRoute.getIsMenu()));
            sysPermission.setType(type.getValue());
            sysPermission.setPath(sysRoute.getPath());
            sysPermission.setPermissionCode(type.getPrefix()+sysPermissionCreateVo.getPermissionCode());
        }

        sysPermission.setPermissionName(sysPermissionCreateVo.getPermissionName());

        boolean exists = this.baseMapper.exists(new LambdaQueryWrapper<SysPermission>().eq(SysPermission::getPermissionCode,sysPermission.getPermissionCode()));

        if(exists){
            throw new GuiguBusinessException("编码已经存在，请使用新的编码");
        }
        sysPermission.setRouteIdPath( routeIdPath );
        sysPermission.setDesc(sysPermissionCreateVo.getDesc());
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(permission_id) as permission_id");
        SysPermission permission = this.getOne(queryWrapper);
        Integer maxId = permission.getPermissionId();
        sysPermission.setPermissionId(maxId+1);
        this.save(sysPermission);

        guiguPickEventPublisher.publish(this,new SysPermissionAddInfo(sysPermission));

        return GuiguResult.success();
    }


}
